home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-10-15 | 6.1 KB | 189 lines | [TEXT/MPS ] |
- #
- # File: DataUtils.vulib
- #
- # Contains: Contains quick tasks for working with various data. Use Mark menu to
- # reach the desired task. The tasks are commented on what they do.
- #
- # Written by: D Gaxiola
- #
- # Copyright: © 1992 by Apple Computer, Inc., all rights reserved.
- #
- # Change History (most recent first):
- #
- # 8/4/92 DGG renamed Car and Cdr to First and Rest
- # 7/24/92 DGG added Phil Thompson's FuseBigNumbers task
- # added IsSubstring task
- # 6/17/92 DGG creation
- #
- # To Do:
- #
-
- (************************************************************************************
- * Task Abs(number)
- * Will return the absolute value of a given integer.
- ************************************************************************************)
- task Abs(number := 0)
- begin
- if (number < 0)
- return -number;
- else
- return number;
- end;
-
- (************************************************************************************
- * Task ConcatStrings(stringList)
- * Will take a list of strings and return a single string which is the
- * concatination of the other strings.
- ************************************************************************************)
- task ConcatStrings(stringList := {})
- begin
- newString := "";
- numberStrings := card stringList;
- for i := 1 to numberStrings
- newString := newString + stringList[i];
- return newString;
- end;
-
- (************************************************************************************
- * Task AddToList(theElement, theList, toFront)
- * Will add an element to the beginning or end of a given list, returning the
- * resultant list.
- ************************************************************************************)
- task AddToList(theElement := "", theList := {}, toFront := false)
- begin
- if toFront
- return insert(theElement, 1, theList);
- else
- return insert(theElement, ((card theList) + 1), theList);
- end;
-
- (************************************************************************************
- * Task First(theList)
- * This is the equivalent of the car operator from the Scheme language. It will
- * return the first element of a list.
- ************************************************************************************)
- task First(theList)
- begin
- if ((card theList) > 0)
- return theList[1];
- else
- return undefined;
- end;
-
- (************************************************************************************
- * Task Rest(theList)
- * This is the equivalent of the cdr operator from the Scheme language. It will
- * return a list of all the elements in a given list, except the first.
- ************************************************************************************)
- task Rest(theList)
- begin
- if ((card theList) > 1)
- return remove(1, theList);
- else
- return undefined;
- end;
-
- (************************************************************************************
- * Task IsSubstring(theSubString, theString)
- * Given a string and a substring, this task will check to see if the substring
- * exists within the main string. If it does, it will return the starting location
- * within the main string, otherwise it will return false.
- ************************************************************************************)
- task IsSubstring(theSubString := "", theString := "")
- begin
- subLength := card theSubString;
- mainLength := card theString;
- if (subLength > mainLength)
- return false;
- else if (subLength = mainLength)
- if (theSubString = theString)
- return 1;
- else
- return false;
- else
- for mainCounter := 1 to (mainLength - (subLength - 1))
- begin
- subCounter := 1;
- tempCounter := mainCounter;
- if theString[tempCounter] = theSubString[subCounter]
- begin
- while theString[tempCounter] = theSubString[subCounter]
- begin
- subCounter := subCounter + 1;
- tempCounter := tempCounter + 1;
- end;
- if (subCounter = subLength + 1)
- return mainCounter;
- end;
- end;
- return false;
- end;
-
- (************************************************************************************
- * Task PopStack(theStack)
- * Given a stack, this task will return a list of two elements, the newly popped
- * element and the new stack. Since "theStack" is never changed by PopStack, we
- * could use it in this manner:
- * myData := PopStack(myStack)[1];
- * myStack := PopStack(myStack)[2];
- ************************************************************************************)
- task PopStack(theStack := {})
- begin
- twoItemList := {};
- twoItemList := AddToList(First(theStack), twoItemList);
- twoItemList := AddToList(Rest(theStack), twoItemList);
- return twoItemList;
- end;
-
- (************************************************************************************
- * Task PushStack(theElement, theStack)
- * Given an element and a "stack" the element will be "pushed" onto the stack.
- * The new stack is returned.
- ************************************************************************************)
- task PushStack(theElement := "", theStack := {})
- begin
- return AddToList(theElement, theStack, true);
- end;
-
- (************************************************************************************
- * Task MakeAssoc(objectList, dataList)
- * Given two lists of equal size, this will build a list of associations between
- * the two.
- ************************************************************************************)
- task MakeAssoc(objectList := {}, dataList := {})
- begin
- if ((card objectList) = (card dataList))
- begin
- listSize := card objectList;
- assocList := {};
- for i := 1 to listSize
- assocList := AddToList({objectlist[i], dataList[i]}, assocList);
- return assocList;
- end;
- else
- return undefined;
- end;
-
- (************************************************************************************
- * Task SortList(theList)
- * Simple bubble sort routine for sorting lists.
- ************************************************************************************)
- task SortList(theList := {})
- begin
- noChanges := false;
- while (not noChanges)
- begin
- noChanges := true;
- for i := 1 to ((card theList) - 1)
- if theList[i] > theList[i+1]
- begin
- tempMember := theList[i];
- theList := replace( theList[i+1], i, theList );
- theList := replace( tempMember, i+1, theList );
- noChanges := false;
- end;
- end;
- return theList;
- end;
-
-